package com.meteor.downloader.videodownloader.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.text.format.Formatter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import coil.load
import coil.transform.RoundedCornersTransformation
import com.meteor.downloader.videodownloader.R
import com.meteor.downloader.videodownloader.databinding.ItemDownloadingItemBinding
import com.meteor.downloader.videodownloader.mdmGetPxDp
import com.meteor.downloader.videodownloader.mdmdata.DataDownloadingMdm

class AdapterDownloadingItem(
    private val mContext: Context,
    private val mListDoData: MutableList<DataDownloadingMdm>,
    private val mDeleteClick: (Int) -> Unit,
    private val mPauseClick: (Int) -> Unit,
    private val mPlayClick: (Int) -> Unit
) : Adapter<AdapterDownloadingItem.HolderItem>() {
    inner class HolderItem(val mBd: ItemDownloadingItemBinding) :
        ViewHolder(mBd.root)

    override fun onCreateViewHolder(
        parent: ViewGroup, viewType: Int
    ): HolderItem {
        val binding =
            ItemDownloadingItemBinding.inflate(LayoutInflater.from(mContext), parent, false)
        val holderItem = HolderItem(binding)
        binding.ivDelete.setOnClickListener {
            mDeleteClick(holderItem.absoluteAdapterPosition)
        }
//        binding.ivMdmPauseStart.setOnClickListener {
//            mPauseClick(holderItem.absoluteAdapterPosition)
//        }
        holderItem.itemView.setOnClickListener {
            val data = mListDoData[holderItem.absoluteAdapterPosition]
            if (data.mProgressMdm == 100) {
                mPlayClick(holderItem.absoluteAdapterPosition)
            }
        }
        return holderItem
    }

    override fun getItemCount(): Int {
        return mListDoData.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: HolderItem, position: Int
    ) {
        val data = mListDoData[holder.absoluteAdapterPosition]
        holder.mBd.apply {
            ivImageVideo.load(data.videoImageAnyMdm ?: data.videoImageMdm) {
                transformations(RoundedCornersTransformation(12f.mdmGetPxDp()))
                placeholder(R.drawable.mdm_shape_16dp_dcdcdc)
                error(R.drawable.mdm_shape_16dp_dcdcdc)
            }
            tvVideoName.text = data.videoNameMdm
            tvMdmSize.text = "${
                Formatter.formatFileSize(
                    mContext,
                    data.mSizeVideoMdm
                )
            }/${Formatter.formatFileSize(mContext, data.mTotalSizeMdm)} | ${data.videoQualityMdm}"
            crvProgress.setProgress(data.mProgressMdm.toFloat())
            if (data.mProgressMdm == 100) {
                crvProgress.visibility = View.GONE
                ivDelete.visibility = View.GONE
            } else {
                crvProgress.visibility = View.VISIBLE
                ivDelete.visibility = View.VISIBLE
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: HolderItem, position: Int, payloads: MutableList<Any>
    ) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads)
        } else {
            val v = payloads[0]
            if (v is Boolean && v) {
                val data = mListDoData[holder.absoluteAdapterPosition]
                holder.mBd.apply {
                    tvMdmSize.text = "${
                        Formatter.formatFileSize(
                            mContext,
                            data.mSizeVideoMdm
                        )
                    }/${
                        Formatter.formatFileSize(
                            mContext,
                            data.mTotalSizeMdm
                        )
                    } | ${data.videoQualityMdm}"
                    crvProgress.setProgress(data.mProgressMdm.toFloat())
                    if (data.mProgressMdm == 100) {
                        crvProgress.visibility = View.GONE
                        ivDelete.visibility = View.GONE
                        tvPlay.visibility = View.VISIBLE
                    } else {
                        crvProgress.visibility = View.VISIBLE
                        ivDelete.visibility = View.VISIBLE
                        tvPlay.visibility = View.GONE
                    }
                }
            }
        }
    }
}